package com.xinqi.modules.chat.chat.message.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.user.Account;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.modules.chat.chat.message.convert.ChatEmojiMessageConvert;
import com.xinqi.modules.chat.chat.message.domain.ChatEmojiMessageEntity;
import com.xinqi.modules.chat.chat.message.dto.req.ChatEmojiMessageCreateDTO;
import com.xinqi.modules.chat.chat.message.dto.req.ChatEmojiMessageDeleteDTO;
import com.xinqi.modules.chat.chat.message.dto.rsp.ChatEmojiMessageResultDTO;
import com.xinqi.modules.chat.chat.message.manager.ChatEmojiMessageManager;
import com.xinqi.modules.chat.chat.message.service.ChatEmojiMessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 聊天消息自定义表情表服务层实现
 *
 * @author: yyzhen
 * @date: 2023/07/11
 */
@Service("chatEmojiMessageService")
@RequiredArgsConstructor
public class ChatEmojiMessageServiceImpl implements ChatEmojiMessageService {
    private final ChatEmojiMessageConvert chatEmojiMessageConvert;
    private final ChatEmojiMessageManager chatEmojiMessageManager;

    /**
     * 新增自定义表情
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(ChatEmojiMessageCreateDTO dto, Account user) {
        ChatEmojiMessageEntity entity = chatEmojiMessageConvert.create(dto);
        entity.setUserId(user.getId());
        chatEmojiMessageManager.save(entity);
        return entity.getId();
    }

    /**
     * 移动到前面
     *
     * @param id
     * @return
     */
    @Override
    public Boolean moveToFront(Long id, Account user) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), MessageUtils.message("chat.message.emoji.id.not.blank"));
        LambdaUpdateWrapper<ChatEmojiMessageEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ChatEmojiMessageEntity::getUpdateTime, LocalDateTime.now())
            .eq(ChatEmojiMessageEntity::getUserId, user.getId())
            .eq(ChatEmojiMessageEntity::getId, id);
        return chatEmojiMessageManager.update(updateWrapper);
    }

    /**
     * 删除自定义表情
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(ChatEmojiMessageDeleteDTO dto) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(dto), MessageUtils.message("chat.message.emoji.id.not.blank"));
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(dto.getId()), MessageUtils.message("chat.message.emoji.id.not.blank"));
        return this.chatEmojiMessageManager.deleteById(dto.getId());
    }

    /**
     * 查询用户的自定义表情
     *
     * @param userId
     * @return
     */
    @DS("slaver")
    @Override
    public List<ChatEmojiMessageResultDTO> listByUserId(Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(userId), MessageUtils.message("chat.message.emoji.userId.not.blank"));
        return chatEmojiMessageManager.listByUserId(userId).stream()
            .map(chatEmojiMessageConvert::convert).collect(Collectors.toList());
    }

}
